package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.atguigu.jxc.util.StringUtil;
import com.sun.org.apache.xml.internal.security.Init;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Shine
 * @description：
 * @date ：2021/3/22 20:33
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    @Override
    public Map<String, Object> listCustomer(Integer page, Integer rows, String customerName) {
        Map<String, Object> map = new HashMap<>();

        // 客户列表分页（名称模糊查询）
        page = page == 0 ? 1 : page;
        int offSite = (page - 1) * rows;
        List<Customer> customerList = customerDao.selectList(offSite, rows, customerName);

        int total = 0;
        for (int i = 0; i < customerList.size(); i++) {
            total++;
        }
        map.put("total", total);
        map.put("rows", customerList);
        return map;
    }

    @Override
    public ServiceVO saveCustomer(Customer customer) {
        // if (customer.getCustomerId() == null) {
        //     return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        // }

        if (customer.getCustomerId() != null) {
            customerDao.update(customer);
        } else {
            customerDao.insert(customer);
        }

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO delete(String ids) {
        if (StringUtil.isEmpty(ids)) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
        String[] split = ids.split(",");
        customerDao.delete(split);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
